var image = {
  add : function(click_source, p_id, x, y) {
    var add_image = $("#add_image");
    var form = add_image.find("form")[0];
    if (!add_image || !form) {
      return;
    }
    add_image.css("left", x + Click.getHtmlElementAbsoluteCoordinates(click_source).x);
    add_image.css("top", y + Click.getHtmlElementAbsoluteCoordinates(click_source).y);
    var pi = add_image.find("input[name=p_id]")[0];
    if (!pi) {
      return;
    }
    pi.value = p_id;
    $.ajax({
      url : "/image/get_upload_url",
      success : function(data, textStatus, jqXHR) {
        if (data.status != "ok" || data.results.url == null) {
          alert("Error getting upload url");
          return;
        }
        form.action = data.results.url;
        add_image.show();
      }
    });
  },

  remove : function(id) {
    if (!confirm("Are you sure you want to delete this image?")) {
      return;
    }
    $.ajax({
      url : "/image/remove?id=" + id,
      type : "POST",
      success : function() {
        window.location.reload(true);
      }
    });
  },

  editProperty : function(id, property_name, property_value) {
    $.ajax({
      url : "/image/edit_property?id=" + id +
        "&property_name=" + property_name + "&property_value=" + property_value,
      type : "post",
      success : function() {
//        alert("success!");
      }
    });
  }
};